# -*- coding: utf-8 -*-
"""
Created on Thu Aug 25 10:24:47 2022

@author: xiong
"""

import logging
import sys
import threading
import time
import queue
logger = logging.getLogger("facebook_spider")
formatter = logging.Formatter("%(asctime)s %(levelname)-8s: %(message)s")
file_handler = logging.FileHandler("facebook_spider.log")
file_handler.setFormatter(formatter)  
console_handler = logging.StreamHandler(sys.stdout)
console_handler.formatter = formatter 
logger.addHandler(file_handler)
logger.addHandler(console_handler)
logger.setLevel(logging.INFO)

####################################


from MqConsumer import MqConsumer
from YmlReader import YmlReader


def new_consumer(i,username,password,host,port,virtual_host,queue):
    
    exec("MqConsumer%s=MqConsumer(username,password,host,port,virtual_host,queue)"%str(i))
    exec("MqConsumer%s.start_consuming()"%str(i))

def start_thread(thread):
    thread.start()

class ExcThread(threading.Thread):
    def __init__(self, target, *args, **kwargs):
        super(ExcThread, self).__init__(*args, **kwargs)
        self.callable_obj = target
        # 退出状态值
        self.exit_code = 0
        self.exception = None

        # 主动设置为守护线程,必须条件
        self.setDaemon(True)
 
    def run(self):
        try:
            self._run()
        except Exception as e:
            self.exit_code = 1
            self.exception = e

 
    def _run(self):
        try:
            self.callable_obj(*self._args, **self._kwargs)
        except Exception as e:
            raise e
            
            
      
            
if __name__ == "__main__":
    
    reader = YmlReader()
    username,password,host,port,virtual_host,queue = (reader.settings["rabbitmq"]["username"],
                                                          reader.settings["rabbitmq"]["password"],
                                                          reader.settings["rabbitmq"]["host"],
                                                          reader.settings["rabbitmq"]["port"],
                                                          reader.settings["rabbitmq"]["virtual-host"],
                                                          reader.settings["rabbitmq"]["queue"],
                                                            )
    maxlimit = int(reader.settings["task_limit"])
    thread_task_list = []
    for i in range(maxlimit):
        t = ExcThread(target=new_consumer,args = (i,username,password,host,port,virtual_host,queue))
        t.start()
        thread_task_list.append(t)
        logger.info("consumer" + str(i+1) + " successfully started")
        
    while True:
        time.sleep(100)
        left_thread = []
        for i in thread_task_list:
            if i.exit_code == 0:
                left_thread.append(i)
                
            else:
                pass
                #i.close()
                
        length = len(left_thread)
        if length < maxlimit :
            for i in range(maxlimit-length):
                logger.info("a thread has quited")
                t = ExcThread(target=new_consumer,args = (i,username,password,host,port,virtual_host,queue))
                t.start()
                left_thread.append(t)
        thread_task_list = left_thread[:]
        logger.info("waiting...")
    #MqConsumer = MqConsumer(username,password,host,port,virtual_host,queue)
    #MqConsumer.start_consuming()